package threadpool;

import java.time.LocalDateTime;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author chaird
 * @create 2023-07-11 21:47
 */
public class ThreadPoolScheduledDemo {
  public static void main(String[] args)  throws Exception{

      System.out.println("main开始执行"+LocalDateTime.now());

    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

    scheduledExecutorService.scheduleAtFixedRate(
        new Runnable() {
          @Override
          public void run() {
              String trace = UUID.randomUUID().toString();
              System.out.println(trace + "开始执行"+LocalDateTime.now());
            try { TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); } finally { }
            // 将数据缓存至本地
            System.out.println("init cache");
              System.out.println(trace + "执行结束"+LocalDateTime.now());
          }
        },
        3,
        10,
        TimeUnit.SECONDS);
  }
}
